﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using System.Web.UI.DataVisualization.Charting;
using System.Data;
using System.IO;

namespace OA.Controllers
{
    public class GoodsChart_twoController : Controller
    {
        //
        // GET: /GoodsChart_two/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GoodsChart_two()
        {
            return View();
        }

        public FileResult Pie()
        {
            goodsInfoManager GIManager = new goodsInfoManager();
            DataTable dt;
            dt = GIManager.getbySelect() ;
            decimal zonghe = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                zonghe = zonghe + decimal.Parse(dt.Rows[i]["销售额"].ToString());
            }
            List<string> name = new List<string>();
            List<string> bili = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {


                string aa = ((100 / zonghe) * decimal.Parse(dt.Rows[i]["销售额"].ToString())).ToString();

                bili.Add(aa);
                name.Add(dt.Rows[i]["类型"].ToString() + " 比例：" + decimal.Parse(aa).ToString("F1") + "%");

            }
            Chart chart1 = new Chart();
            chart1.Height = 450;
            chart1.Width = 850;
            chart1.RenderType = System.Web.UI.DataVisualization.Charting.RenderType.ImageTag;
            chart1.Palette = ChartColorPalette.BrightPastel;
            Title t = new Title("商品饼图", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
            chart1.Titles.Add(t);
            chart1.ChartAreas.Add("view");
            chart1.Series.Add("Series1");
            chart1.ChartAreas["view"].Area3DStyle.Enable3D = true;
            chart1.ChartAreas[0].AxisX.Interval = 1;
            chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            // CustomerChart.BackColor = System.Drawing.Color.Red;
            chart1.BorderlineWidth = 2;
            chart1.BorderColor = System.Drawing.Color.Black;
            chart1.BorderlineDashStyle = ChartDashStyle.Solid;
            chart1.BorderWidth = 1;
            chart1.Series["Series1"].ChartType = SeriesChartType.Pie;
            chart1.Series["Series1"]["PieLabelStyle"] = "Outside";
            chart1.Series["Series1"]["PieLinelColor"] = "Red";
            chart1.Series[0].Points.DataBindXY(name, bili);
            chart1.DataBind();
            MemoryStream imageStream = new MemoryStream();
            chart1.SaveImage(imageStream, ChartImageFormat.Png);
            imageStream.Position = 0;
            return new FileStreamResult(imageStream, "image/png");
        }
    }
}
